"use strict";	
var app = angular.module("myapp", []);
		app.controller("TeachController", function($scope) {
			$scope.teachs = eval(teachlist);
		});
		app.controller("pptController",function($scope){
			$scope.ppts = [];
		});
		app.controller("CoursewareController", function($scope,$http) {
			$scope.coursewares = eval(coursewarelist);
			$scope.mousein = function(row) {
				$scope.selectedRow = row;
			};
			$scope.download = function(row) {
				location.href = "download?fileID=" + $scope.coursewares[row].id;
			};
			$scope.remove = function(row) {
				//delete/courseware?id={{row.coursewareID}}
				var url = "delete/courseware?id="+$scope.coursewares[row].coursewareID;
				console.log("请求地址："+url);
				location.href = url;
			/*	$http.get('delete/courseware',{id:$scope.coursewares[row].coursewareID}).success(function(data) {
					console.log("请求结果："+data);
					
				}).error(function(data,status,headers,config) {
					
				});*/
	
				
			};
			$scope.preview = function(row) {
				var obj = $scope.coursewares[row];
				var isSupported = false;
				switch (obj.type) {
				case "image/jpeg":
				case "image/png":
				case "image/gif":
					$('#preview_img').attr("src","preview?id="+obj.id);
					$('#preview_img_download_btn').attr("href","download?fileID="+obj.id);
					$('#modal_img').modal();
					isSupported = true;
					break;
				case "text/xml":
					console.log("这是xml");
					break;
				case "application/msword":
					console.log("这是doc");
					//break;
				case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
					console.log("这是docx");
					//break;
				case "text/plain":
				case "application/octet-stream":
					$('#preview_text').html("正在加载。。。");
					$('#preview_text_download_btn').attr("href","download?fileID="+obj.id);
					$('#modal_text').modal();
					$http.get('http://localhost:8080/erms/preview',{params:{id:obj.id}}).success(function(data,status,headers,config) {
						 var res = eval("("+eval(data)+")");
						//console.log("请求结果："+res);
						$('#preview_text').html(res.data);
						
					}).error(function(data,status,headers,config) {
						console.log("请求失败："+data);
					});
					
					isSupported = true;
					console.log("这是普通文本");
					break;
				case "application/pdf":
					console.log("这是PDF");
					break;
				case "application/vnd.ms-powerpoint":
					console.log("这是ppt");
					$('#preview_ppt_loading').html("正在加载。。。");
					$('#preview_ppt_download_btn').attr("href","download?fileID="+obj.id);
					$('#modal_ppt').modal();
					$http.get('preview',{params:{id:obj.id}}).success(function(data,status,headers,config) {
						 var res = eval("("+data+")");
						console.log("请求结果："+res);
						 $('#preview_ppt_loading').empty();
							var scope1 = angular.element($("#preview_ppt")).scope();
							scope1.ppts = eval(res);
							scope1.$apply();
							console.log("ppt="+scope1.ppts);
					}).error(function(data,status,headers,config) {
						console.log("请求失败："+data);
					});
					isSupported = true;
					break;
				case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
					console.log("这是pptx");
					break;
				case "application/vnd.ms-excel":
					console.log("这是xls");
					break;
				case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
					console.log("这是xlsx");
					break;
				case "application/zip":
					console.log("这是zip");
					break;
				/*case "application/octet-stream":
					console.log("任意二进制");
					break;*/
				default:
					break;
				}
				console.log(obj.type);
				if (!isSupported) {					
					alert("暂不支持"+obj.type+"类型的预览");
				}
			};
			console.log($scope.coursewares);
		});

		//文件上传
		$(function() {
			// $('input[type=file]').bootstrapFileInput();
			var new_file = $('#new_file_btn');
			var reset_btn = $('#reset_btn');
			var isOpened = false;
			new_file.click(function() {
				if (isOpened) {
					$(this).html("<span class=\"glyphicon glyphicon-plus\"></span>&nbsp;新增课件");
				} else {
					$(this).html("<span class=\"glyphicon glyphicon-minus\"></span>&nbsp;取消上传");
					reset_btn.click();
				}
				isOpened = !isOpened;
			});

			var bar = $('.progress-bar');
			var status = $('#status');
			var percentValue = $('#percentValue');
			$('#new_courseware_file').ajaxForm({
				beforeSend : function() {
					status.empty();
					percentValue.empty();
					var percentVal = '0%';
					percentValue.html(percentVal);
					bar.width(percentVal);
				},
				uploadProgress : function(event, position, total, percentComplete) {
					var percentVal = percentComplete + '%';
					percentValue.html(percentVal);
					bar.width(percentVal);
				},
				success : function() {
					var percentVal = '100%';
					percentValue.html(percentVal);
					bar.width(percentVal);
					status.html("上传完成，正在处理... 请稍等");
				},
				complete : function(xhr) {
					console.log(xhr.responseText);
					var res = eval("(" + eval(xhr.responseText) + ")");
					console.log(res);
					if (res.error == 1) {
						// alert(res.msg);
						$("#submit-btn").removeAttr("disabled");
						$("#submit-btn").removeClass("disabled");
						percentValue.html('0%');
						bar.width('0%');
						status.html(res.msg);
					} else {
						new_file.click();
						percentValue.html('0%');
						bar.width('0%');
						status.empty();
						var scope = angular.element($("#hello")).scope();
						scope.coursewares.splice(0, 0, res.courseware);
						scope.$apply();
					}

					// status.html("");

				}
			});

		});